﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PI_CS_Library.PicsNetwork_Information_Model;
using PI_CS_Library.Virtual_Network_Information_Model;
using PI_CS_Library;
using System.Windows.Forms.DataVisualization.Charting;

namespace PI_CS_GUI
{
    public partial class PhysicalLinkGUI : Form
    {
        GUIDelegate _gui;

        PicsLink _reqLink;
        PicsLink _revLink;

        int EDGE = 454;

        PicsNetwork _network;

        Dictionary<string, int> _vnoUsage;
        Dictionary<int, VirtualNetworkOperatorInfo> VNOperators;

        public PhysicalLinkGUI(int _linkId, PicsNetwork _network, GUIDelegate _gui, Dictionary<int, VirtualNetworkOperatorInfo> VNOperators)
        {
            InitializeComponent();

            this._gui = _gui;
            this._network = _network;
            this.VNOperators = VNOperators;

            _reqLink = _network.GetLink(_linkId);

            _revLink = IsBidirectional(_reqLink, _network);

            label1.Text = _reqLink.SrcMatrix.Name + " -> " + _reqLink.SinkMatrix.Name;
            label1.Location = new Point(EDGE - label1.Size.Width, label1.Location.Y);

            GetLinkDetails(_reqLink);

        }

        public void GetLinkDetails(PicsLink _physicalLink)
        {
            int _vlinkId, _vnId, _vnoId;

            _vnoUsage = new Dictionary<string, int>();

            listView1.Items.Clear();

            if (_physicalLink.HostedLinkList.Count == 0)
            {
                chart1.Enabled = false;
                listView1.Enabled = false;
            }
            else
            {
                chart1.Enabled = true;
                listView1.Enabled = true;

                foreach (Link l in _physicalLink.HostedLinkList.Values)
                {
                    _vlinkId = _network.LinkMappingList[l.Id.Number];
                    _vnId = _network.VirtualLinkList[_vlinkId].vnId;
                    _vnoId = _network.HostedNetworkList[_vnId].VnoId.Number;


                    ListViewItem item = new ListViewItem(new[] { VNOperators[_vnoId].Login, Convert.ToString(_vnId), Convert.ToString(l.Id.Number) });
                    listView1.Items.Add(item);

                    if (_vnoUsage.ContainsKey(VNOperators[_vnoId].Login))
                        _vnoUsage[VNOperators[_vnoId].Login] += l.Parameters.Capacity;
                    else
                        _vnoUsage.Add(VNOperators[_vnoId].Login, l.Parameters.Capacity);
                }


                chart1.Series.Clear();

                Series series = chart1.Series.Add("usage");
                series.ChartType = SeriesChartType.Bar;

                foreach (string _name in _vnoUsage.Keys)
                    series.Points.AddXY(_name, _vnoUsage[_name]);

                series.Points.AddXY("Unused", _physicalLink.Parameters.AvailableCapacity);
            }

            label5.Text = Convert.ToString(_physicalLink.Parameters.Capacity);
            label7.Text = Convert.ToString(_physicalLink.Parameters.Delay);
            label9.Text = Convert.ToString(_physicalLink.Parameters.ErrorRate);
        }

        public PicsLink IsBidirectional(PicsLink _reqLink, PicsNetwork _network)
        {
            PicsMatrix A = _reqLink.SrcMatrix;
            PicsMatrix B = _reqLink.SinkMatrix;

            foreach (PicsLink l in _network.LinkResourceList.Values)
                if (l.SrcMatrix == B && l.SinkMatrix == A) return l;

            return null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = _reqLink.SrcMatrix.Name + " <- " + _reqLink.SinkMatrix.Name;
            label1.Location = new Point(EDGE - label1.Size.Width, label1.Location.Y);
            GetLinkDetails(_revLink);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            label1.Text = _reqLink.SrcMatrix.Name + " -> " + _reqLink.SinkMatrix.Name;
            label1.Location = new Point(EDGE - label1.Size.Width, label1.Location.Y);
            GetLinkDetails(_reqLink);
        }

        private void PhysicalLinkGUI_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListViewItem item = listView1.SelectedItems[0];
            Link _vlink = _network.GetVirtualNetwork(Int32.Parse(item.SubItems[1].Text)).GetLink(Int32.Parse(item.SubItems[2].Text));

            VirtualLinkGUI _panel = new VirtualLinkGUI(_vlink.SrcMatrix.Id.Number, _vlink.SinkMatrix.Id.Number, Int32.Parse(item.SubItems[1].Text), _network, _gui);
            _panel.ShowDialog();
        }
    }
}
